<template>
  <!--弹出窗-->
  <modal ref="modal_1"
         :title="title"
         :isCloseConfirm="this.editable"
         :fullscreen="this.fullscreen"
         top="10%"
         width="50%"
         :close-on-click-modal="true"
         @modalOpened="opened"
         @modalClose="closed">

    <avue-form :option="option"
               ref="form"
               v-model="form"
               @submit="onSubmit">
    </avue-form>

    <!--底部按钮块, 默认只有"取消"按钮-->
    <template #footerButton>
      <el-button
              type="primary"
              v-if="(permission.work_list_add || permission.work_list_edit) && editable"
              :loading="btnLoading"
              @click="this.$refs.form.submit()"
      >保 存
      </el-button>
      <el-button @click="$refs.modal_1.handleClose()">关闭</el-button>
    </template>
  </modal>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/anwise/work_list";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        emits: ["success"],
        editable: false,
        title: "工序信息",
        isCloseConfirm: false,
        fullscreen: false,
          btnLoading: false,
        form: {},
        query: {},
        loading: true,
        option: {
          height: 'auto',
          calcHeight: 30,
          labelWidth: 150,
          menuWidth: 120,
          submitBtn: false,
          emptyBtn: false,
          column: [
              {
                  label: "工序名称",
                  prop: "workName",
                  type: "input",
                  maxlength:500,
                  rules: [{
                      required: true,
                      message: "请输入工序名称",
                      trigger: "blur"
                  }],
              },
              {
                  label: "刀模说明",
                  prop: "dmRemark",
                  type: "textarea",
                  minRows:2,
                  maxlength: 2000,
                  sortable: true,
              },
              {
                  label: "机器说明",
                  prop: "jqRemark",
                  type: "textarea",
                  minRows:2,
                  maxlength: 2000,
                  sortable: true,
              },
              {
                  label: "备注",
                  prop: "remark",
                  type: "textarea",
                  minRows:2,
                  maxlength: 2000,
                  sortable: true,
              },
              {
                  label: "创建人员",
                  prop: "createUserName",
                  type: "input",
                  display:false,
                  disabled:true,
              },
              {
                  label: "创建时间",
                  prop: "createTime",
                  type: "input",
                  display:false,
                  disabled:true,
              },
          ]
          },
          data: []
        };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.validData(this.permission.work_list_add, false),
          viewBtn: this.validData(this.permission.work_list_view, false),
          delBtn: this.validData(this.permission.work_list_delete, false),
          editBtn: this.validData(this.permission.work_list_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
        onSubmit(form, done) {
           this.btnLoading = true;
           update(form).then(res => {
               if (res.data.success) {
                   this.$message({
                       type: "success",
                       message: res.msg || "保存成功!"
                   });
                   setTimeout(() => {
                     this.btnLoading = false;
                     done();
                       this.$refs.modal_1.hide();
                   }, 1000)
               } else {
                   this.$message({
                       type: "error",
                       message: res.msg || "保存失败!"
                   });
               }
           }).catch(err => {
               console.log(err)
               done();
               this.btnLoading = false;
           })
        },
        show(option) {
            console.log(option)
            if (option.id) {
                setTimeout(() => {
                    getDetail(option.id).then(res =>{
                        if (res.data.success) {
                            this.form = res.data.data;
                            this.findObject(this.option.column, 'createUserName').display=true;
                            this.findObject(this.option.column, 'createTime').display=true;
                        }
                    })
                }, 500);
            } else {
                this.form={}
                this.findObject(this.option.column, 'createUserName').display=false;
                this.findObject(this.option.column, 'createTime').display=false;
            }
            this.title = option.title;
            this.type = option.type;
            this.option.detail = this.type === "view";
            this.editable = this.type !== "view";
            this.$refs.modal_1.show();
        },
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      },
        opened() {
            console.log("opened")
        },
        closed() {
            this.$emit("success");
        }
    }
  };
</script>

<style>
</style>
